﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CodeAuto.DALS
{
    public class SQLDALS
    {

        public static string GetMSSQLString(Models.T_Table table, List<Models.T_Field> fields)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("             CREATE TABLE [" + table.TableName + "](<br/>");
            foreach (Models.T_Field field in fields)
            {
                if (field == fields.First())
                {
                    sb.AppendLine("[" + field.Field + "] [int] IDENTITY(1,1) NOT NULL,<br/>");
                }
                else
                {
                    if (field.Attr.Equals("string"))
                    {
                        sb.AppendLine("[" + field.Field + "] [nvarchar](255) COLLATE Chinese_PRC_CI_AS NULL,<br/>");
                    }
                    if (field.Attr.Equals("DateTime"))
                    {
                        sb.AppendLine("[" + field.Field + "] [datetime] NULL,<br/>");
                    }
                    if (field.Attr.Equals("int"))
                    {
                        sb.AppendLine("[" + field.Field + "] [int] NULL,<br/>");
                    }
                    if (field.Attr.Equals("double"))
                    {
                        sb.AppendLine("[" + field.Field + "] [float] NULL,<br/>");
                    }
                }

            }
            string s = sb.ToString();
            s = s.Substring(0, s.Length - 8);
            s += "<br/>CONSTRAINT [PK_" + table.TableName + "] PRIMARY KEY CLUSTERED<br/>";
            s += "(<br/>";
            s += "	[" + fields[0].Field + "] ASC<br/>";
            s += ")WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)<br/>";
            s += ") ON [PRIMARY]<br/>";
            return s;
        }

        public static string GetAccessString(Models.T_Table table, List<Models.T_Field> fields)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("create table [" + table.TableName + "](<br/>");
            foreach (Models.T_Field field in fields)
            {
                if (field == fields.First())
                {
                    sb.Append("[" + field.Field + "]  autoincrement(1,1) primary key,<br/>");
                }
                else
                {
                    if (field.Attr.Equals("string"))
                    {
                        sb.Append("[" + field.Field + "] text(255),<br/>");
                    }
                    if (field.Attr.Equals("DateTime"))
                    {
                        sb.Append("[" + field.Field + "] DateTime,<br/>");
                    }
                    if (field.Attr.Equals("int"))
                    {
                        sb.Append("[" + field.Field + "] integer,<br/>");
                    }
                    if (field.Attr.Equals("double"))
                    {
                        sb.Append("[" + field.Field + "] Float,<br/>");
                    }
                }

            }
            string s = sb.ToString();
            s = s.Substring(0, s.Length - 6);
            s += ")<br/>";
            return s;
        }

        public static string GetMySqlString(Models.T_Table table, List<Models.T_Field> fields)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();


            return sb.ToString();
        }

        public static string GetSqliteString(Models.T_Table table, List<Models.T_Field> fields)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("create table [" + table.TableName + "](<br/>");
            foreach (Models.T_Field field in fields)
            {
                if (field == fields.First())
                {
                    sb.AppendLine("[" + field.Field + "]   INTEGER PRIMARY KEY AUTOINCREMENT,<br/>");
                }
                else
                {
                    if (field.Attr.Equals("string"))
                    {
                        sb.AppendLine("[" + field.Field + "] nvarchar(255),<br/>");
                    }
                    if (field.Attr.Equals("DateTime"))
                    {
                        sb.AppendLine("[" + field.Field + "] datetime,<br/>");
                    }
                    if (field.Attr.Equals("int"))
                    {
                        sb.AppendLine("[" + field.Field + "] integer,<br/>");
                    }
                    if (field.Attr.Equals("double"))
                    {
                        sb.AppendLine("[" + field.Field + "] double,<br/>");
                    }
                }

            }
            string s = sb.ToString();
            s = s.Substring(0, s.Length - 8);
            s += ")<br/>";
            return s;
        }
    }
}